﻿/*
 * GlobalIndicator.cs
 * 
 * alexis 20111211
 * */

using Microsoft.Phone.Shell;
using System.ComponentModel;
using Microsoft.Phone.Controls;
using System.Windows.Navigation;
using System.Windows.Threading;
using System.Windows.Media;
using System;
using System.Windows;

namespace Cnblogs
{
    public class GlobalIndicator 
    {
        private ProgressIndicator _mangoIndicator;
        private PhoneApplicationPage CurrentPage;

        private GlobalIndicator()
        {
            
        }
        private static GlobalIndicator _in;
        public static GlobalIndicator Instance
        {
            get
            {
                if (_in == null)
                {
                    _in = new GlobalIndicator();
                }

                return _in;
            }
        }

        public void Initialize(PhoneApplicationFrame frame)
        {
            _mangoIndicator = new ProgressIndicator();
            frame.Navigating += new NavigatingCancelEventHandler(frame_Navigating);
            frame.Navigated += OnRootFrameNavigated;
        }

        void frame_Navigating(object sender, NavigatingCancelEventArgs e)
        {
            if (_mangoIndicator != null && _mangoIndicator.IsVisible)
            {
                _mangoIndicator.Text = "";
                _mangoIndicator.IsIndeterminate = false;
            }
        }

        private void OnRootFrameNavigated(object sender, NavigationEventArgs e)
        {
            var ee = e.Content;
            var pp = ee as PhoneApplicationPage;
            CurrentPage = pp;
            if (pp != null)
            {
                pp.SetValue(SystemTray.ProgressIndicatorProperty, _mangoIndicator);
            }
        }

        public void ShowMessage(string message,int seconds=2)
        {
            StartBusy(message, false);
            DispatcherTimer _timer = new DispatcherTimer();
            _timer.Interval = TimeSpan.FromSeconds(seconds);
            _timer.Tick += (o, e) =>
                {
                    StopBusy();
                    if (_timer!=null)
                    {
                        _timer.Stop();
                        _timer = null;
                    }
                };
            _timer.Start();
        }

        public void StartBusy(string message = "", bool isIndeterminate = true)
        {
            if (_mangoIndicator == null)
            {
                _mangoIndicator = new ProgressIndicator();
            }
            Deployment.Current.Dispatcher.BeginInvoke(delegate
            {
                _mangoIndicator.Text = message;
                _mangoIndicator.IsIndeterminate = isIndeterminate;
                _mangoIndicator.IsVisible = true;
            });
        }

        public void StopBusy()
        {
            Deployment.Current.Dispatcher.BeginInvoke(delegate
            {
                _mangoIndicator.Text = "";
                _mangoIndicator.IsIndeterminate = false;
                _mangoIndicator.IsVisible = false;
            });
        }        
    }
}
